package com.admin.boot.modules.department.controller;

import com.admin.boot.common.config.FileUploadConfig;
import com.admin.boot.common.utils.Result;
import com.admin.boot.modules.department.entity.SysDepartment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @version v3.0
 * @Description: TODO
 * @author: jeecg-boot
 * @date: 2024-01-15
 */
@RestController
@RequestMapping("/sys/fileUpload")
@Api("文件上传")
public class FileUploadController {

    @Autowired
    private FileUploadConfig fileUploadConfig;


    @PostMapping("/upload")
    @ApiOperation(value = "文件上传-上传", notes = "文件上传-上传")
    public Result<?> upload(MultipartFile file) throws IOException {
        String uploadPath = fileUploadConfig.getUploadPath();
        File dir = new File(uploadPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        String originalFilename = file.getOriginalFilename();
        String randomStr = UUID.randomUUID().toString().substring(0, 32);
        String fileName = randomStr + "_" + originalFilename;
        String s = uploadPath + "/" + fileName;
        File file1 = new File(s);
        file.transferTo(file1);
        return Result.OK("http://localhost:9999/api"+fileUploadConfig.getStaticFileMapping()+"/"+fileName);
    }

}
